<template>
  <!-- 新闻详情页 -->
  <div class="articaldetail">
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back" @click="$router.back()" />
        <span class="iconfont iconnew new"></span>
      </div>
      <span :class="{ follow: post.has_follow }" @click="focus">{{
        post.has_follow ? "已关注" : "关注"
      }}</span>
    </div>
    <div class="detail">
      <div class="title">{{ post.title }}</div>
      <div class="desc">
        <span>{{ post.user && post.user.nickname }}</span> &nbsp;&nbsp;
        <span>{{ post.create_date | istime_filter }}</span>
      </div>
      <div class="content" v-html="post.content" v-if="post.type == 1"></div>

      <video
        class="content"
        :src="axios.defaults.baseURL + post.content"
        v-if="post.type == 2"
        controls
        poster="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2597842340,4269352535&fm=26&gp=0.jpg"
      ></video>

      <div class="opt">
        <span class="like" :class="{ give_like: post.has_like }" @click="like">
          <van-icon name="good-job-o" />{{ post.like_length }}
        </span>
        <span class="chat"> <van-icon name="chat" class="w" />微信 </span>
      </div>
    </div>

    <!-- 精彩跟帖 -->
    <myarticles :article="post"></myarticles>
  </div>
</template>

<script>
import { istime_filter } from "@/utils/time_filter.js";
import axios from "@/utils/myaxios.js";
import myarticles from "@/components/myarticles.vue";
import { user_follows, user_unfollow, post_like, postId } from "@/apis/post.js";

export default {
  components: {
    myarticles,
  },
  data() {
    return {
      axios,
      post: {},
    };
  },
  filters: {
    istime_filter,
  },
  async activated() {
    setTimeout(() => {
      window.scrollTo(0, 0);
    }, 0);
    // 根据id获取文章的详情，实现文章详情的动态渲染
    let id = this.$route.params.id;
    let res = await postId(id);
    this.post = res.data.data;
  },
  methods: {
    async focus() {
      let id = this.post.user.id;
      let res;
      if (this.post.has_follow) {
        res = await user_unfollow(id);
      } else {
        res = await user_follows(id);
      }
      this.post.has_follow = !this.post.has_follow;

      this.$toast.success(res.data.message);
    },
    async like() {
      let id = this.post.id;
      let res = await post_like(id);
      if (res.data.message == "取消成功") {
        this.post.like_length--;
      } else {
        this.post.like_length++;
      }
      console.log(res);
      this.post.has_like = !this.post.has_like;
      console.log(this.post.has_like);
      this.$toast.success(res.data.message);
    },
  },
};
</script>

<style lang='less' scoped>
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #fc0808;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
  }
  .follow {
    background-color: #ccc;
    color: #fc0808;
  }
}

.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    /deep/ img {
      width: 100%;
      display: block;
    }
  }
}
.give_like {
  color: #f30909;
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>
